//
//  ViewController.m
//  09-HomeWork
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CityViewController.h"
#define kTableViewCellReuse @"reuse"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *provinceArray;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"省份";
    [self loadData];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuse];
}
#pragma mark - 点击推出市页面方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  //获取市数组
    NSMutableArray *cityArr = [self.provinceArray[indexPath.row] objectForKey:@"cityArray"];
    CityViewController *cityVC = [[CityViewController alloc] init];
    cityVC.provinceName = [_provinceArray[indexPath.row] objectForKey:@"provinceName"];
    cityVC.cityArray = cityArr;
    [self.navigationController pushViewController:cityVC animated:YES];
}



#pragma mark- dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _provinceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.textLabel.text = [_provinceArray[indexPath.row] objectForKey:@"provinceName"];
    return cell;
}

#pragma mark - loadData
- (void)loadData {
    _provinceArray = [NSMutableArray array];
    NSString *sourceString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
    NSArray *sourceArray = [sourceString componentsSeparatedByString:@"\n"];
    for (NSString *string in sourceArray) {
        if (![string hasPrefix:@" "]) {
         //是省名
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            //存省名
            [provinceDic setObject:string forKey:@"provinceName"];
            // 创建市数组并存储
            NSMutableArray *cityArray = [NSMutableArray array];
            [provinceDic setObject:cityArray forKey:@"cityArray"];
            [_provinceArray addObject:provinceDic];
        } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
         //是市名
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:string forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            
            NSMutableArray *cityArray = [[_provinceArray lastObject] objectForKey:@"cityArray"];
            [cityArray addObject:cityDic];
        } else {
        // 是区名
            NSMutableArray *cityArray = [[_provinceArray lastObject] objectForKey:@"cityArray"];
            NSMutableArray *zoneArray = [[cityArray lastObject] objectForKey:@"zoneArray"];
            [zoneArray addObject:string];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
